<?php
    session_start();
    require_once("errorhandler.php");
    set_error_handler("error_msg");
    
    $sporten = array("Klimmen", "Tennis", "Voetbal", "Tafeltennis", "Biljart");
    $dagen = array("Maandag", "Dinsdag", "Woensdag", "Donderdag", "Vrijdag");
    
    $formFields = array(
        "Voornaam" => array("tekst" => "", "error" => "", "verplicht" => "*"), "Tussenvoegsel" => array("tekst" => "", "error" => "", "verplicht" => "")
        , "Achternaam" => array("tekst" => "", "error" => "", "verplicht" => "*"), "Straat" => array("tekst" => "", "error" => "", "verplicht" => "")
        , "Huisnummer" => array("tekst" => "", "error" => "", "verplicht" => ""), "Postcode" => array("tekst" => "", "error" => "", "verplicht" => "")
        , "Woonplaats" => array("tekst" => "", "error" => "", "verplicht" => ""), "Email" => array("tekst" => "", "error" => "", "verplicht" => "*")
        , "Geboortedatum" => array("tekst" => "", "error" => "", "verplicht" => " (dd-mm-jjjj)")
        , "Ingangsdatum" => array("tekst" => "", "error" => "", "verplicht" => "*  (dd-mm-jjjj)")
        ,    "Einddatum" => array("tekst" => "", "error" => "", "verplicht" => " (dd-mm-jjjj)"));

    // Vul array $formFields met de actuele waarden uit de sessie variabelen.
    foreach ($formFields as $fieldName => &$fieldValue) {
        if (isset($_SESSION[$fieldName])) $fieldValue["tekst"] = $_SESSION[$fieldName];
        if (isset($_SESSION[$fieldName . "Error"])) $fieldValue["error"] = $_SESSION[$fieldName . "Error"];
    }
    unset($fieldValue); 
    
    if (isset($_SESSION["Geslacht"])) {
        if ($_SESSION["Geslacht"] == "M") {
            $man = "checked";
            $vrouw = "";
        } elseif ($_SESSION["Geslacht"] == "V") {
            $man = "";
            $vrouw = "checked"; 
        } else {
            $man = "";
            $vrouw = "";
        }
    } else {
        $man = "";
        $vrouw = "";
    }
    
    isset($_SESSION["Sportonderdeel"]) ? $Sportonderdeel = $_SESSION["Sportonderdeel"] : $Sportonderdeel = "";
    isset($_SESSION["SportonderdeelError"]) ? $SportonderdeelError = $_SESSION["SportonderdeelError"] : $SportonderdeelError = "";
    isset($_SESSION["Lesdag"]) ? $Lesdag = $_SESSION["Lesdag"] : $Lesdag = "";

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
        <title>Inzendopdracht 051R6</title>
	</head>
<body>
	<div id="wrap">
		<div id="header">
            <P><h2>Wil je sporten? doe het nu!<BR /><BR />
                Vul dit formulier in om je aan te melden voor een Sport</h2></P>
            <p>Alle velden met een (*) zijn verplicht.</p>
        </div>
        <div id="rechterImage">
            <BR />
        </div>
    <FORM ACTION="verwerk.php" METHOD="POST" />
        <ul id="contactform">
            <?php
            // print alle enkelvoudige formulier velden
            foreach ($formFields as $fieldName => $fieldValue) {
                $fieldText = $fieldValue["tekst"];
                $fieldError = $fieldValue["error"];
                $verplicht = $fieldValue["verplicht"];
                echo "<li>\n";
                echo "<label for='$fieldName'>$fieldName $verplicht</label>\n";
                echo "<span class='fieldbox'><input type='text' name='$fieldName' id='$fieldName' value='$fieldText'/></span>\n";
                echo "<span class='errormessage'>$fieldError</span>\n";
                echo "</li>\n";
            }
            ?>
            <li>
                <label for="geslacht">Geslacht</label>
                <span class="checkbox">
                <input type="radio" name="Geslacht" value="M" <?php echo $man; ?> >Man<br>
                <input type="radio" name="Geslacht" value="V" <?php echo $vrouw; ?> >Vrouw
                </span>
            </li>        
            
            <li>
                <label for="sportonderdeel">Sportonderdeel*</label>
                <span class="fieldbox"><select NAME="Sportonderdeel">
                    <OPTION value="Empty"></option>
                    <?php foreach($sporten as $sport)
                        if ($sport == $Sportonderdeel) {
                            echo "<OPTION value='$sport' selected>$sport</option>\n";
                        } else {
                            echo "<OPTION value='$sport'>$sport</option>\n";
                        }
                        echo "</select></span>\n";
                        echo "<span class='errormessage'>$SportonderdeelError</span>\n";
                    ?>
            </li>
            <li>
                <label for="Lesdag">Lesdag</label>
                <span class="fieldbox"><select NAME="Lesdag">
                <OPTION value="Empty"></option>
                    <?php foreach($dagen as $dag)
                        if ($dag == $Lesdag) {
                            echo "<OPTION value='$dag' selected>$dag</option>\n";
                        } else {
                            echo "<OPTION value='$dag'>$dag</option>\n";
                        }
                    ?>
            </select></span>
            </li>
        </ul>
        <P><INPUT TYPE="submit" value="Aanmelden" id="sendbutton" name="sendbutton"/></P>
        <P><INPUT TYPE="submit" value="Reset" id="resetbutton" name="resetbutton"/></P>
    </FORM>
        </div>
  </body>
</html>